package com.kin.rich.service;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import org.cyberneko.html.parsers.DOMParser;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.kin.rich.pojo.Company;
import com.kin.rich.pojo.Keyword;
import com.kin.rich.pojo.Project;
import com.kin.rich.pojo.Searchdata;
import com.kin.rich.pojo.User;
import com.kin.rich.util.CalendarFunction;


public class BaiduWebSpider extends AbstractSpider implements Runnable{
	
	private String string = "";
	
	private Keyword initKeyword;
	
	public BaiduWebSpider(){
		
	}
	
	public BaiduWebSpider(Keyword keyword){
		this.initKeyword = keyword;
	}
	public void run(){
		this.search(initKeyword);
	}
	
	public void search(Keyword keyword) {
		//log.error("start");
		String word = keyword.getWordName();
		int pageNum = keyword.getCountPageNo();
		log.error("页数："+pageNum);
		int wordId = keyword.getWordId();
		try{
			String uri = "http://www.baidu.com/s?wd="+URLEncoder.encode(word, "GBK")+"&pn=";
			Date date = new Date();
			String today = CalendarFunction.dateToString(date);
			//检查今日是否有搜索
			//System.out.println("ssss"+helper==null);
			int searchTimes = helper.getSearchTimes(wordId, today, "BaiduWeb");
			log.error("searchTimes   :"+searchTimes);
			searchTimes = searchTimes >=0 ? (searchTimes+1) : 1;
			log.error("searchTimes::::"+searchTimes);
			for(int i=1;i<=pageNum;i++){
				log.error("第"+i+"次连接请求");
					string = "";
					URL url = new URL(uri+(i-1)*10);
					log.error("url:"+url);
					HttpURLConnection connection = (HttpURLConnection)url.openConnection();
					connection.setConnectTimeout(5000);
					InputStreamReader in = new InputStreamReader(connection.getInputStream(),"GB18030");
					
					DOMParser parser = new DOMParser();
			        parser.setProperty("http://cyberneko.org/html/properties/default-encoding","GB18030");
			        parser.setFeature("http://cyberneko.org/html/features/augmentations", true);
			        parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
			        
			        parser.parse(new InputSource(in));
			        Document doc = parser.getDocument();
			        Node html = doc.getElementsByTagName("html").item(0);
			        Searchdata source = new Searchdata();
			        source.setWordId(wordId);
			        source.setDataSource(this.writeDoc(html));
			        string = "";
			        source.setDataPageNo(i);
			        source.setLinkUrl(uri+(i-1)*10);
			        source.setDataTitle("百度网页搜索——"+word+"——第"+i+"页");
					source.setDataCat(10);
					source.setDataContent(html.getTextContent());
					source.setDataIndexNo(0);
					source.setSearchDate(date);
					source.setSearchTimes(searchTimes);
					source.setUserChecked(10);
					source.setSource("BaiduWeb");
					
					srhDAO.save(source);
					for(int j=1;j<=10;j++){
						int index = j+(i-1)*10;
						Node table = doc.getElementById(""+index);
						if(table!=null){
							//table.get
							NodeList tbodyList = table.getChildNodes();
							log.error(this.writeDoc(table));
							int item = 0;
							Node tbody = tbodyList.item(0);
							while(!tbody.getNodeName().toUpperCase().equals("TBODY")){
								tbody = tbodyList.item(++item);
							}
							log.error("item:"+item);
							NodeList nodeList = tbody.getChildNodes();
					        Node tr = tbody.getFirstChild();
					        item =0;
					        while(!tr.getNodeName().toUpperCase().equals("TR"))
					        	tr = nodeList.item(++item);
					        Node td = tr.getFirstChild();
					        NodeList tdList = td.getChildNodes();
					        Node h3 = null,font = null;
					        for(int thisI=0;thisI<tdList.getLength();thisI++){
					        	Node node = tdList.item(thisI);
					        	if(node.getNodeName().toUpperCase().equals("H3"))
					        		h3 = node;
					        	else if(node.getNodeName().toUpperCase().equals("FONT"))
					        		font = node;
					        }
					        Node a = h3.getFirstChild();
					        /*if(!a.getNodeName().toUpperCase().equals("A")){
					        	a = tdList.item(++item);
					        }*/
					        log.error("a:"+a.getNodeValue());
					        String surl = a.getAttributes().getNamedItem("href").getTextContent();

					        Searchdata data = new Searchdata();
							data.setDataContent(table.getTextContent());
							data.setDataIndexNo(index);
							data.setDataPageNo(i);
							data.setDataSource(this.writeDoc(table));
							string = "";
							data.setDataTitle(a.getTextContent());
							log.error(a.getNodeValue());
							data.setSea_data_id(source.getDataId());
							data.setSearchDate(date);
							data.setSearchTimes(searchTimes);
							data.setWordId(wordId);
					        data.setLinkUrl(surl);
					        data.setSource("BaiduWeb");
							
					        int checked = helper.checkDataCat(surl, data.getDataTitle(), wordId);
					        
							data.setUserChecked(checked);
							data.setDataCat(checked>0 ? 1 : 0);
							if(font != null){
							    NodeList list = font.getChildNodes();//td.getChildNodes().item(2).getChildNodes();
							    for(int k=0;k<list.getLength();k++){
							    	Node l = list.item(k);
							    	if(l.getNodeName().toUpperCase().equals("A")){
							    		String baidukuaizhao = l.getAttributes().getNamedItem("href").getTextContent();
							        		if(baidukuaizhao.indexOf("cache.baidu.com")>=0){
							        			log.error(baidukuaizhao);
							        			data.setBaiduCache(baidukuaizhao);
							        		}else data.setBaiduCache(surl);
							        		break;
							        	}
							   }
							}
					        srhDAO.save(data);
						}else return ;
					}
			}
			}catch(Exception e){
			e.printStackTrace();
		}
		return ;
	}
	
	
	private String writeDoc(Node node) {
		  short type = node.getNodeType();
		  switch (type) {

		  case Node.ELEMENT_NODE: {
		   String name = "<" + node.getNodeName();
		   NamedNodeMap attrs = node.getAttributes();
		   if (attrs != null) {
		    int length = attrs.getLength();
		    for (int i = 0; i < length; i++) {
		     Node attr = attrs.item(i);
		     name += " " + attr.getNodeName();
		     name += "=\"" + attr.getNodeValue() + "\"";
		    }
		   }
		   name += ">";
		   string += name;
		   //System.out.println(name);

		   NodeList children = node.getChildNodes();
		   if (children != null) {
		    int length = children.getLength();
		    for (int i = 0; i < length; i++)
		     writeDoc(children.item(i));
		   }
		   
		   string += ("</" + node.getNodeName() + ">");
		   break;
		  }
		  case Node.TEXT_NODE: {
		   string += (node.getNodeValue());
		   break;
		  }
		  }
		  return string;
		 }
	
	public void init(){
		User user = new User();
		user.setRegDate(new Date());
		user.setUserName("tuxiliang");
		user.setUserPwd("123456");
		user.setEndDate(new Date());
		//userDAO.save(user);
		
		Company comp = new Company();
		comp.setCompEnddate(CalendarFunction.StringToDate("2010-09-09"));
		comp.setCompName("法尔胜");
		comp.setJoinDate(new Date());
		comp.setUserId(user.getUserId());
		companyDAO.save(comp);
		
		Project proj = new Project();
		proj.setCompId(comp.getCompId());
		proj.setProjAddDate(new Date());
		proj.setProjDesc("项目描述");
		proj.setProjEnddate(CalendarFunction.StringToDate("2010-09-09"));
		proj.setProjName("公司关键词优化");
		proj.setUserId(user.getUserId());
		projectDAO.save(proj);
		Project proj2 = new Project();
		proj2.setCompId(comp.getCompId());
		proj2.setProjAddDate(new Date());
		proj2.setProjDesc("项目描述");
		proj2.setProjEnddate(CalendarFunction.StringToDate("2010-09-09"));
		proj2.setProjName("个人关键词优化");
		proj2.setUserId(user.getUserId());
		projectDAO.save(proj2);
		
		
		Keyword keyword = new Keyword();
		keyword.setAddWordDate(new Date());
		keyword.setCountPageNo(10);
		keyword.setProjId(proj.getProjId());
		keyword.setUserId(user.getUserId());
		keyword.setWordName("法尔胜");
		keyword.setEndWordDate(new Date());
		keywordDAO.save(keyword);
		
		Keyword keyword2 = new Keyword();
		keyword2.setAddWordDate(new Date());
		keyword2.setCountPageNo(10);
		keyword2.setProjId(proj.getProjId());
		keyword2.setUserId(user.getUserId());
		keyword2.setWordName("法尔胜周建松");
		keywordDAO.save(keyword2);
		
		Keyword keyword3 = new Keyword();
		keyword3.setAddWordDate(new Date());
		keyword3.setCountPageNo(10);
		keyword3.setProjId(proj2.getProjId());
		keyword3.setUserId(user.getUserId());
		keyword3.setWordName("周建松");
		keywordDAO.save(keyword3);
		
		
	}
	
	public static BaiduWebSpider getFromApplicationContext(ApplicationContext ctx) {
		return (BaiduWebSpider) ctx.getBean("BaiduWebSpider");
	}
	public static void main(String[] args) {
		ApplicationContext appContext = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
		BaiduWebSpider spider =  BaiduWebSpider.getFromApplicationContext(appContext);
		//Keyword keyword = spider.keywordDAO.findById(4);
		//spider.search(keyword);
		//spider.runSpiderOneByOne();
		//spider.runSpiderWithThread();
		//spider.init();
	}
	
}
